$(function () {
    var lossStu_datagrid = $("#lossStu_datagrid");
    var lossStu_dialog = $("#lossStu_dialog");
    var lossStu_add_form = $("#lossStu_add_form");

    lossStu_datagrid.datagrid({
        url: "/lossStu/query",
        fit: true,
        fitColumns: true,
        fit: true,
        rownumbers: true,
        columns: [[
            {field: "name", title: "学生姓名", width: 100},
            {field: "tel", title: "联系电话", width: 100},
            {field: "classStatus", title: "退学阶段", width: 100, formatter: formatterClassStatus},
            {field: "inputEmployee", title: "营销人员", width: 100, formatter: formatterInputEmployee},
            {field: "removeStuTime", title: "退学时间", width: 125},
            {field: "reason", title: "退学原因", width: 100},
            {field: "inClassDays", title: "上课天数", width: 100},
            {field: "statusEmployee", title: "经办人员", width: 100, formatter: formatterStatusEmployee},
            {field: "refund", title: "是否退款", width: 100,formatter:formatterRefund},
            {field: "audit", title: "审核状态", width: 100, formatter: formatterAudit}
        ]],
        toolbar: "#lossStu_toolbar",
        singleSelect: true,
        pagination: true
    });
    //编辑弹框
    lossStu_dialog.dialog({
        buttons: "#lossStu_dialog_buttons",
        closed: true
    });

    //1.把方法绑定到一个对象上
    var cmdObj = {
        //学员退学编辑
        editLossStu: function () {
            //获取选中行
            var selectedRow = lossStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            if(selectedRow.audit == 0) {
                //设置表单回显
                //清空表单
                lossStu_add_form.form("clear");
                //数据回显
                //营销人员回显
                if (selectedRow.inputEmployee) {
                    selectedRow['inputEmployee.id'] = selectedRow.inputEmployee.id;
                }
                // //经办人员回显
                // if (selectedRow.statusEmployee) {
                //     selectedRow['statusEmployee.id'] = selectedRow.statusEmployee.id;
                // }

                lossStu_add_form.form("load", selectedRow);
                //弹出编辑框
                lossStu_dialog.dialog("setTitle", "编辑");
                lossStu_dialog.dialog("open");
                //冻结编辑缴费信息框
            }else{
                $.messager.alert("提示","该["+selectedRow.name+"]学员已被审核,无法编辑","error");
            }
        },
        //学员退学加载
        reloadLossStu: function () {
            lossStu_datagrid.datagrid("reload");
        },
        //提交保存
        save: function () {
            //提交表单
            lossStu_add_form.form("submit", {
                url: "/lossStu/update",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadLossStu();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            });
        },
        //取消
        cancel: function () {
            //关闭添加弹窗
            lossStu_dialog.dialog("close");
        },
        //高级查询
        search: function () {
            //1.获取查询input里面的值
            var keyWord = $("#keyWord").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            var classStatus = $("#classStatus").val();

            //2.调用datagrid重新加载数据,并且带上查询条件  keyword className beginDate endDate
            lossStu_datagrid.datagrid("load", {
                keyWord: keyWord,
                beginDate: beginDate,
                endDate: endDate,
                classStatus: classStatus
            });
        },
        //审核
        auditLossStu: function () {
            //获取选中行
            var selectedRow = lossStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            if (selectedRow.audit == 0) {
                $.messager.confirm("提示", "确定要审核当前学员吗?", function (yes) {
                    if (yes) {
                        //上课天数没有超过7天 并且没退款
                        if ((!selectedRow.refund) && selectedRow.inClassDays <= 7) {
                            $.messager.alert("提示", "当前学员[" + selectedRow.name + "] 上课天数为["+selectedRow.inClassDays+"]天,满足退款条件而没退款,当前无法审核","error");
                        }else if(selectedRow.refund || ((!selectedRow.refund) && selectedRow.inClassDays > 7)){
                            $.post("/lossStu/auditLossStu", {id: selectedRow.id}, function (data) {
                                if (data.success) {
                                    $.messager.alert("提示", "审核成功", "info", function () {
                                        cmdObj.reloadLossStu();
                                    });
                                } else {
                                    $.messager.alert("提示", data.msg, "error");
                                }
                            });
                        }
                    }
                });
            } else {
                $.messager.alert("提示", "禁止重复审核", "error");
            }
        },
        //反审核
        unauditLossStu: function () {
            //获取选中行
            var selectedRow = lossStu_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }

            if (selectedRow.audit == 1) {
                $.messager.confirm("提示", "确定要反审核当前学员吗?", function (yes) {
                    if (yes) {
                        $.post("/lossStu/unauditLossStu", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "反审核成功", "info", function () {
                                    cmdObj.reloadLossStu();
                                });
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        });
                    }
                });
            } else {
                $.messager.alert("提示", "当前学员还未审核,请审核后再试", "error");
            }
        }
    }


    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });


});

//格式化营销人员
function formatterInputEmployee(value, row, index) {
    return value == null ? "" : value.realName;
}

//格式化经办人员
function formatterStatusEmployee(value, row, index) {
    return value == null ? "" : value.realName;
}

//格式化班级状态
function formatterClassStatus(value, row, index) {
    if (value == 0) {
        return "基础班";
    } else {
        return "<span style='color: #d9534f'>大神班</span>";
    }
}

//格式化是否退款
function formatterRefund(value,row,index) {
    if (value) {
        return "是";
    }else{
        return "否";
    }
}

//格式化审核状态
function formatterAudit(value, row, index) {
    if (value == 0) {
        return "<span style='color: #d9534f'>未审核</span>";
    } else {
        return "已审核";
    }
}
